package com.house365.core.bean;

import java.io.Serializable;
import java.lang.reflect.Field;

import android.database.Cursor;

import com.house365.core.reflect.ReflectException;
import com.house365.core.reflect.ReflectUtil;

/**
 *@author py
 *@date 2012-6-7
 *@comment
 */
public abstract class BaseBean implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = -3228723769762271774L;
	public static boolean isContainColumn(String colName, Cursor rs) {

		boolean flag = false;
		for (String s : rs.getColumnNames()) {
			if (s.equalsIgnoreCase(colName)) {
				flag = true;
				break;
			}
		}
		return flag;
	}

	public void readFromCursor(Cursor cursor){
			Class clazz = this.getClass();
			Field[] fields = clazz.getDeclaredFields();
			String name;
			for(Field f: fields){
				f.setAccessible(true);
				name = f.getName();
				if(isContainColumn(name,cursor)){
						int idx = cursor.getColumnIndex(name);
						String s = cursor.getString(idx);
						if(s != null){
							if(ReflectUtil.isSimpleDataType(f.getType())){
								try {
									f.set(this,ReflectUtil.getObjectFromString(f.getType(),s));
								} catch (IllegalArgumentException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} catch (IllegalAccessException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} catch (ReflectException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
						}
						
				}
				
		}
	}
}
